<?php

class Logmodel extends CI_Model{
	
	var $conn;
	
	function __construct()
    {
        // Call the Model constructor
        parent::__construct();
		$user = "automatsystem";
		$pass = "freemachineskillhumans";
		error_reporting(0);
		$this->conn = mysql_connect("localhost:3306",$user,$pass);
		if(!$this->conn){
			$this->conn = mysql_connect("localhost:3307",$user,$pass);	
		}
		if(!$this->conn){
			$this->conn = mysql_connect("localhost:3308",$user,$pass);	
		}
		if(!$this->conn){
			$this->conn = mysql_connect("localhost:3309",$user,$pass);
		}
		error_reporting(E_ALL);
		if(!$this->conn){
			echo '*ERROR* Usermodel: Can\'t connect to MySQL server.';
		}
		else{
			mysql_select_db("automatsystem");
		}
    }

	function testConnection(){
		if($this->conn){return true;}
		return false;
	}
	
	/*
		������� ����� � ������� ������� Log. ����� ������ ��� ������� : LogId, AutomatId, WareId, DateTime
		������� ������������:
			$query = $this->Logmodel->getLog();
			echo $query[0]['LogId'];
	*/
	function getLog(){
		$queryRes = mysql_query("SELECT * FROM Log;",$this->conn);
		$retArr = array();
		while($row = mysql_fetch_assoc($queryRes)){
			$retArr[] = $row;
		}
		return $retArr;
	}
	
        function getAutomatLog($automatId){
		$queryRes = mysql_query("SELECT * FROM Log WHERE AutomatId=".$automatId.";",$this->conn);
		$retArr = array();
		while($row = mysql_fetch_assoc($queryRes)){
			$retArr[] = $row;
		}
		return $retArr;
	}
        
	//
	function log($automatId, $wareId){
		$res = mysql_query("INSERT INTO Log(AutomatId, WareId, DateTime)
		VALUES('".$automatId."','".$wareId."',NOW());",$this->conn);
		if(!$res){echo '<p>Error adding log. </p>';}
		return $res;
	}
	
	
}